package com.yanqu.road.server.cmd.kuang;

import com.yanqu.road.entity.kuang.KuangConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.kuang.CrossKuangMgr;
import com.yanqu.road.server.manager.kuang.activity.CrossKuangActivity;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

@Cmd(code = CrossProtocol.C_CROSS_KUANG_LOGIN_SYNC, desc = "跨服取下活动状态")
public class CrossKuangLoginSyncCmd extends CrossAppointCommand {

    private int groupId;

    private PlayerProto.PlayerBaseTempMsg userBaseInfoTemp = null;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        Kuang.CrossKuangLoginSyncMsg reqMsg = Kuang.CrossKuangLoginSyncMsg.parseFrom(packet.getBytes());
        groupId = reqMsg.getGroupId();
        userBaseInfoTemp = reqMsg.getUserBaseInfo();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossKuangActivity activity = CrossKuangMgr.getActivity(groupId);
        if (activity == null) {
            ret = GameErrorCode.E_KUANG_BATTLE_ACTIVITY_NOT_FOUND;
            return;
        }
        KuangConfig config = activity.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_KUANG_CONFIG_NOT_FOUND;
            return;
        }

        // 更新userBaseInfo
        if (userBaseInfoTemp != null) {
            UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(userBaseInfoTemp);
            CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);
        }


        activity.crossBattleSync(userId);
    }

    @Override
    public void sendData() {

    }
}
